package validate

import (
	"crypto/tls"
	// "encoding/json"
	"fmt"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/httplib"

	log "github.com/cihub/seelog"
)

var (
	validateURL string = "https://127.0.0.1:443/v1/validate/user"
)

//只解析部分字段
type LoginValidateResp struct {
	Code   int    `json:"code"`
	Msg    string `json:"msg"`
	Userid string `json:"userid"`
}

func LoginValidate(uid, ak, token string) *LoginValidateResp {
	reqURL := fmt.Sprintf("%s?appkey=ab848a4efb08ff31eb8d900ae1858b42", validateURL)
	b := httplib.Post(reqURL)
	b.SetHost("ppswitch.app.qkier.com")
	b.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
	body := make(map[string]interface{})
	body["token"] = token
	body["uid"] = uid
	body["ak"] = ak
	b.JSONBody(body)

	resp := &LoginValidateResp{}
	if err := b.ToJSON(resp); err != nil {
		log.Errorf("request to old passport get userinfo err. err: %s", err.Error())
		return nil
	}
	return resp
}

func init() {
	validateURL = beego.AppConfig.DefaultString("http::validateURL", "https://127.0.0.1:443/v1/validate/user")
}
